/*
 * Copyright (c) 2007, Nubo Technologies
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 *    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.nubotech.gwt.oss.browser.client.ui;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.nubotech.gwt.oss.client.SecurityPolicy;
import com.nubotech.gwt.oss.client.auth.AuthenticationManager;
import com.nubotech.gwt.oss.client.ui.FileUploadFormPanel;

/**
 *
 * @author jonnakkerud
 */
public class UploadFileWidget extends Composite {
    private FileUploadFormPanel form;
    private BrowserView browserView;
    
    public UploadFileWidget(final BrowserView browserView) {
        this.browserView = browserView;
        VerticalPanel panel = new VerticalPanel();
        form = FileUploadFormPanel.instance(panel, AuthenticationManager.instance().getLogin());
        form.addFormHandler(new FormHandler() {
            public void onSubmit(FormSubmitEvent evt) {
            }
            public void onSubmitComplete(FormSubmitCompleteEvent evt) {
                DeferredCommand.addCommand(new Command() {
                    public void execute() {
                        browserView.refresh();
                    }
                });

            }
        });
        initWidget(form);
        initGui(panel);
    }
    
    
    private void initGui(VerticalPanel panel) {        

        final ListBox lb = new ListBox();
        lb.addItem("Private");
        lb.addItem("Public Read");
        lb.addItem("Public Read/Write");
        lb.addItem("Authenticated Read");
        panel.add(lb);
       
        // Create a FileUpload widget.
        final FileUpload upload = new FileUpload();
        upload.setName("file");
        panel.add(upload);
        
        panel.add(new HTML("<br>"));
        
        // Add a 'submit' button.
        panel.add(new Button("Submit", new ClickListener() {
            public void onClick(Widget sender) {
                // bucket
                form.setBucketName(browserView.pathBar.getBucket());

                // key
                form.setKey(getKey());
        
                // set the policy
                form.setPolicy(SecurityPolicy.AUTO);
                
                form.setAccessPolicy(lb.getSelectedIndex());
                form.setContentTypeFromFile(upload.getFilename());
                form.submit();                
            }
        }));        
    }
    
    private String getKey() {
        return browserView.pathBar.getPrefix();
    }
    
}
